﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ExtremeML.Spreadsheet.RichText
{
	/// <summary>
	/// Stores the the vertical properties for a rich text run.
	/// </summary>
	internal class AlignmentDefinition
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="AlignmentDefinition"/> class.
		/// </summary>
		/// <param name="properties">A <see cref="RunProperties"/> instance from which to import values.</param>
		public AlignmentDefinition(
			RunProperties properties)
		{
			if (properties == null) throw new ArgumentNullException("properties");

			Alignment = properties.GetFirstChild<VerticalTextAlignment>();
		}

	
		/// <summary>
		/// Initializes a new instance of the <see cref="AlignmentDefinition"/> class.
		/// </summary>
		/// <param name="markup">A string containing markup that defines the properties of the new instance.</param>
		public AlignmentDefinition(
			string markup)
		{
			Parse(markup);
		}

		/// <summary>
		/// Gets a <see cref="VerticalTextAlignment"/> that represents the vertical text alignment.
		/// </summary>
		public VerticalTextAlignment Alignment { get; private set; }

		/// <summary>
		/// Applies the values stored in this instance to the passed <see cref="RunProperties"/> instance.
		/// </summary>
		/// <param name="properties">A <see cref="RunProperties"/> instance to be populated.</param>
		public void Populate(
			RunProperties properties)
		{
			properties.RemoveAllChildren<VerticalTextAlignment>();

			if (Alignment != null) properties.AppendChild((VerticalTextAlignment)Alignment.Clone());
		}

		/// <summary>
		/// Parses the passed font definition markup string, applying values to the properties of this instance.
		/// </summary>
		private void Parse(
			string markup)
		{
			const string pattern = @"(?<attribute>[\w]+)=""(?<value>[^""]+)""";

			foreach (Match match in Regex.Matches(markup, pattern))
			{
				switch (match.Groups["attribute"].Value.ToLower())
				{
					case "value":
						switch (match.Groups["value"].Value.ToLower())
						{
							case "none":
								Alignment = new VerticalTextAlignment {Val = VerticalAlignmentRunValues.Baseline};
								break;
							case "subscript":
								Alignment = new VerticalTextAlignment {Val = VerticalAlignmentRunValues.Subscript};
								break;
							case "superscript":
								Alignment = new VerticalTextAlignment {Val = VerticalAlignmentRunValues.Superscript};
								break;
							default:
								break;
						}

						break;
					default:
						break;
				}
			}
		}
	}
}
